home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 264_01 / vis.c < prev    next >
Text File  |  1980-01-01  |  2KB  |  100 lines

  1. /*
  2.  * vis - visual display of files
  3.  *
  4.  * Usage: vis [-et] [file...]
  5.  *
  6.  * Flags:
  7.  * -e    show the ends of lines as '$'
  8.  * -t    show tabs as '^I'
  9.  *
  10.  * Contents of argument files are written to standard output.
  11.  * If there are no arguments, or if a file named "-" is encountered,
  12.  * standard input is written to standard output.
  13.  * Exit status is number of files that couldn't be opened or had read errors.
  14.  *
  15.  * This program is in the public domain.
  16.  * David MacKenzie
  17.  * 6522 Elgin Lane
  18.  * Bethesda, MD 20817
  19.  *
  20.  * Latest revision: 05/08/88
  21.  */
  22.  
  23. #include <stdio.h>
  24.  
  25. _main(argc, argv)
  26.     int     argc;
  27.     char  **argv;
  28. {
  29.     int     showtabs = 0;    /* show tabs as ^I? */
  30.     int     showend = 0;    /* show eol as $? */
  31.     int     optind;        /* loop index */
  32.     int     errs = 0;        /* # of files with read errors */
  33.  
  34.     for (optind = 1; optind < argc && *argv[optind] == '-'; ++optind)
  35.     while (*++argv[optind])
  36.         switch (*argv[optind]) {
  37.         case 'e':
  38.         showend = 1;
  39.         break;
  40.         case 't':
  41.         showtabs = 1;
  42.         break;
  43.         default:
  44.         fprintf(stderr, "Usage: vis [-et] [file...]\n");;
  45.         exit(1);
  46.         break;
  47.         }
  48.  
  49.     if (optind == argc)
  50.     errs += vis("-", showtabs, showend);
  51.     else
  52.     for (; optind < argc; ++optind)
  53.         errs += vis(argv[optind], showtabs, showend);
  54.  
  55.     exit(errs);
  56. }
  57.  
  58. /*
  59.  * Visually display file on standard output with newline and ^Z
  60.  * conversions. If file is "-", use standard input.
  61.  * Return 0 if ok, 1 if error.
  62.  */
  63.  
  64. vis(file, showtabs, showend)
  65.     char   *file;
  66.     int     showtabs, showend;
  67. {
  68.     FILE   *fp = stdin;        /* input file pointer */
  69.     int     c;            /* one byte of input */
  70.  
  71.     if (strcmp(file, "-") && (fp = fopen(file, "r")) == NULL) {
  72.     perror(file);
  73.     return 1;
  74.     }
  75.     while ((c = agetc(fp)) != EOF) {
  76.     if (c > 127) {
  77.         /* Meta-characters. */
  78.         putchar('M');
  79.         putchar('-');
  80.         c &= 127;
  81.     }
  82.     if (c == '\n' && showend) {
  83.         putchar('$');
  84.         putchar('\n');
  85.     } else if (c < ' ' && (c != '\t' || showtabs) && c != '\n') {
  86.         putchar('^');
  87.         putchar(c + 'A' - 1);
  88.     } else if (c == 127) {
  89.         putchar('^');
  90.         putchar('?');
  91.     } else
  92.         putchar(c);
  93.     }
  94.     if (fp != stdin && fclose(fp) == EOF) {
  95.     fprintf(stderr, "%s: Read error\n", file);
  96.     return 1;
  97.     }
  98.     return 0;
  99. }
  100.